<?php

namespace app\common\model;


use think\Model;
use think\Db;

//用户物品模型
class UserGoodsModel extends Model{

    protected $name = 'user_goods';

    //获取好友对战
    public function change_user_goods_record($goods,$user_info,$desc=''){
        $goods_info=Db::name('user_goods')->where(['target_id'=>$goods['target_id'],'target_type'=>$goods['target_type'],'user_id'=> $user_info['id']])->find();
        if($goods_info){
            $goods_number=$goods['goods_number'];
            $goods_data = [
                'num'=>['exp',"num+$goods_number"]
              ];
           $res = Db::name('user_goods')->where(['id'=>$goods_info['id']])->update($goods_data);
        }else{
            $goods_data=[
                'user_id'       => $user_info['id'],
                'target_id'     => $goods['target_id'],
                'target_type'   => $goods['target_type'],
                'num'           => $goods['goods_number'],
                'is_new'        => 1,
                'create_time'   => time()
            ];
           $res =  Db::name('user_goods')->insertGetId($goods_data);
        }
        //用户奖励日志
        $data_reward=[
            'user_id'       => $user_info['id'],
            'user_name'     => $user_info['user_name'],
            'remark'        => $desc,
            'create_time'   => time(),
            'target_id'     => $goods['target_id'],
            'target_type'   => $goods['target_type']
        ];
        Db::name('reward_log')->insertGetId($data_reward);

        return $res ? true : false; 
    }
    
}